package com.sap.jcp.sm.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.hsqldb.lib.StringUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sap.jcp.sm.dao.ShiftDAO;
import com.sap.jcp.sm.dao.ShiftDAOImpl;
import com.sap.jcp.sm.domain.Shift;
import com.sap.jcp.sm.domain.User;
import com.sap.jcp.sm.util.MappingUtil;

public class ShiftAction extends ActionSupport implements ModelDriven<Shift>, SessionAware, RequestAware {

	private Map sessionMap;
	private Map request;
	private static final long serialVersionUID = 1L;
	private static final String LOGINSUCCESS = "loginsuccess";
	private Shift shift = new Shift();
	private List<Shift> shiftList = new ArrayList<Shift>();
	private ShiftDAO shiftDAO = new ShiftDAOImpl();
	private String message;
	private List<User> userList = new ArrayList<User>();
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public void validate() {
		if (StringUtil.isEmpty(shift.getShiftName())) {
			super.addActionError("Shift Name required.");
		}
	}

	public Shift getModel() {
		return shift;
	}
	
	public String addShift() {
		validate();
		if (getActionErrors() != null && !getActionErrors().isEmpty()) {
			return ERROR;
		}
		shift.setDeleteFlag("false");
		setMessage("Shift has been created successfully.");
		shiftDAO.saveShift(shift);
		refreshShift();
		shift = new Shift();
		return SUCCESS;
	}
	private void refreshShift(){
		List<Shift> shiftList = shiftDAO.listShift();
		sessionMap.put("shift", MappingUtil.convertShiftListToMap(shiftList));
		sessionMap.remove("shiftEditInfo");
	}
	
	public String editShift()
	{
		Long shiftId = (Long) request.get("shiftId");
		Shift shiftInfo = shiftDAO.getShift(shiftId);
		sessionMap.put("shiftEditInfo", shiftInfo);
		return SUCCESS;
	}
	
	public String updateShift()
	{
		validate();
		shift.setDeleteFlag("false");
		if (getActionErrors() != null && !getActionErrors().isEmpty()) {
			return ERROR;
		}
		shiftDAO.updateShift(shift);
		refreshShift();
		return SUCCESS;
	}
	public String cancelEdit() {
		sessionMap.remove("shiftEditInfo");
		return SUCCESS;
	}
	
	public String delete()
	{
		Long shiftId = (Long) request.get("shiftId");
		Shift shiftInfo = shiftDAO.getShift(shiftId);
		shiftInfo.setDeleteFlag("true");
		shiftDAO.updateShift(shiftInfo);
		refreshShift();
		return SUCCESS;
	}
	
	public String list() {
		shiftList = shiftDAO.listShift();
		return SUCCESS;
	}
		
	public Shift getShift() {
		return shift;
	}

	public void setShift(Shift shift) {
		this.shift = shift;
	}


	public List<Shift> getShiftList() {
		return shiftList;
	}

	public void setShiftList(List<Shift> shiftList) {
		this.shiftList = shiftList;
	}
	@Override
	public void setSession(Map<String, Object> map) {
		this.sessionMap = map;
	}
	public Map<String, Object> getSession() {
		 return sessionMap;
	}

	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
		
	}

}
